﻿var link_ajaxpostform = function (jo, options) {
    var attrobj = Utils.getAttrObject(jo, "ajaxpostform");
    var form = $(jo).closest("form");
    var vals = form.serializeArray();
    var data = {};
    for (var i = 0; i < vals.length; i++) {
        data[vals[i].name] = vals[i].value;
    }
    var postdata = $.extend({}, data);
    var settings = $.extend({
        datatype: $(jo).attr("datatype") || "json",
        onsuccess: Utils.eval($(jo).attr("onsuccess") || "_hyt_ajax_onsuccess_auto"),
        onfail: Utils.eval($(jo).attr("onfail") || "_hyt_ajax_onfail"),
        onexcption: Utils.eval($(jo).attr("onexcption") || "function(){}"),
        ontimeout: Utils.eval($(jo).attr("ontimeout") || "_hyt_ajax_ontimeout"),
        onservererror: Utils.eval($(jo).attr("onservererror") || "_hyt_ajax_onservererror"),
        action: {}
    }, options, attrobj, data);
    var url = form.attr("action");
    var send = function () {
        $.ajax({
            url: url,
            data: postdata,
            type: "POST",
            dataType: settings.datatype,
            success: function (rspdata, state, rspraw) {
                //var allheaer = rspraw.getAllResponseHeaders();
                var contentype = rspraw.getResponseHeader("Content-Type");
                
                if (contentype.indexOf("application/json") != -1) {
                    if (rspdata.success) {
                        settings.onsuccess(rspdata, rspraw, settings);
                    } else {
                        settings.onfail(rspdata, rspraw);
                    }
                } else if (contentype.indexOf("text/html") != -1) {
                    settings.onsuccess(rspdata, rspraw, settings);
                } else {
                    mAlertManager.error("返回信息不正确");
                }
            },
            error: function (err, errtype) {
                if (errtype == "parsererror") {
                    mAlertManager.error("返回信息不正确");
                } else {
                    settings.onservererror(err, errtype);
                }
            },
            timeout: function () {
                settings.ontimeout();
            }
        });
    };
    if (settings.confirm) {
        mAlertManager.confirm(settings.confirm, function () {
            send();
        });
    } else {
        send();
    }
};

var _hyt_ajax_ontimeout = function () {
    mAlertManager.error("连接服务器超时,请检查你的网络情况");
};
var _hyt_ajax_onservererror = function (err, errtype) {
    mAlertManager.error(err.responseText);
};
var _hyt_ajax_onfail = function (rspdata, rspraw) {
    mAlertManager.error(rspdata.message);
};
var _hyt_ajax_onsuccess_auto = function (rspdata, rspraw, settings) {
    
    var contentype = rspraw.getResponseHeader("Content-Type");
    if (contentype.indexOf("application/json") != -1) {
        if (settings.action.alert) {
            var isobject = typeof (settings.action.alert) == "object"
            mAlertManager.alert(rspdata.message || (isobject && settings.action.alert.message) || "操作成功", (isobject && settings.action.alert.autoclose) || 0);
        }
        if (settings.action.notifyparent) {
            mWindowPointer.up().notify(intent.action.dataischange);
        }
        if (settings.action.closeme) {
            mWindowPointer.notify(intent.action.closewindow);
        }
        if (settings.action.refresh) {
            mWindowPointer.notify(intent.action.refresh);
        }
        if (settings.action.fill) {
            if (settings.action.fill.to) {
                mWindowPointer.notify(intent.action.fill, { to: settings.action.fill.to, content: rspdata.content });
            } else {
                mWindowPointer.notify(intent.action.fill, rspdata.content);
            }
        }
    } else if (contentype.indexOf("text/html") != -1) {
        if (settings.action.alert) {
            var isobject = typeof (settings.action.alert) == "object"
            mAlertManager.alert((isobject && settings.action.alert.message) || "操作成功", (isobject && settings.action.alert.autoclose) || 0);
        }
        if (settings.action.notifyparent) {
            mWindowPointer.up().notify(intent.action.dataischange);
        }
        if (settings.action.closeme) {
            mWindowPointer.notify(intent.action.closewindow);
        }
        if (settings.action.refresh) {
            mWindowPointer.notify(intent.action.refresh);
        }
        if (settings.action.fill) {
            if (settings.action.fill.to) {
                mWindowPointer.notify(intent.action.fill, { to: settings.action.fill.to, content: rspdata.reponseText });
            } else {
                mWindowPointer.notify(intent.action.fill, rspdata.reponseText);
            }
        }

    }
};